//
// Created by amon.huang on 2021/9/11.
//

#include <cstring>
#include <unistd.h>
#include <jni.h>
#include "Push.h"
#include "PushConstDefine.h"

#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>

Push *pPush = nullptr;
PushJNICall *pPushJNICall = nullptr;

#define null (NULL)

JavaVM *pJavaVM = nullptr;

extern "C" JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *vm, void */*reserved*/) {
    LOGE("JNI_OnLoad -->");
    pJavaVM = vm;
    JNIEnv *env;
    if (pJavaVM->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_4) != JNI_OK) {
        return -1;
    }
    return JNI_VERSION_1_4;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_amon_amonplayer_LivePush_nInitConnect(JNIEnv *env, jobject thiz, jstring url_) {
    // TODO: implement nInitConnect()
    const char *url = env->GetStringUTFChars(url_, 0);
    if (!pPush && !pPushJNICall) {
        pPushJNICall = new PushJNICall(pJavaVM, env, thiz);
        pPush = new Push(pPushJNICall, url);
        pPush->initConnect();
        env->ReleaseStringUTFChars(url_, url);
    }
}

extern "C"
JNIEXPORT void JNICALL
Java_com_amon_amonplayer_LivePush_nStop(JNIEnv *env, jobject thiz) {
    // TODO: implement nStop()
    if (pPush) {
        pPush->stop();
        delete pPush;
        pPush = nullptr;
    }

    if (pPushJNICall) {
        delete pPushJNICall;
        pPushJNICall = nullptr;
    }
}

extern "C"
JNIEXPORT void JNICALL
Java_com_amon_amonplayer_LivePush_pushSpsPps(JNIEnv *env, jobject thiz, jbyteArray m_video_sps,
                                             jint sps_length, jbyteArray m_video_pps,
                                             jint pps_length) {
    jbyte *sps = env->GetByteArrayElements(m_video_sps, nullptr);
    jbyte *pps = env->GetByteArrayElements(m_video_pps, nullptr);
    if (pPush) {
        pPush->pushSpsPps(reinterpret_cast<u_char *>(sps), sps_length,
                          reinterpret_cast<u_char *>(pps), pps_length);
    }
    env->ReleaseByteArrayElements(m_video_sps, sps, 0);
    env->ReleaseByteArrayElements(m_video_pps, pps, 0);
}

extern "C"
JNIEXPORT void JNICALL
Java_com_amon_amonplayer_LivePush_pushVideo(JNIEnv *env, jobject thiz, jbyteArray video_data,
                                            jint length, jboolean key_frame) {
    jbyte *videoData = env->GetByteArrayElements(video_data, nullptr);
    if (pPush) {
        pPush->pushVideoData(reinterpret_cast<u_char *>(videoData), length, key_frame);
    }
    env->ReleaseByteArrayElements(video_data, videoData, 0);
}

extern "C"
JNIEXPORT void JNICALL
Java_com_amon_amonplayer_LivePush_pushAudio(JNIEnv *env, jobject thiz, jbyteArray audio_data,
                                            jint length) {
    jbyte *audioData = env->GetByteArrayElements(audio_data, nullptr);
    if (pPush) {
        pPush->pushAudioData(reinterpret_cast<u_char *>(audioData), length);
    }
    env->ReleaseByteArrayElements(audio_data, audioData, 0);
}

extern "C"
JNIEXPORT void JNICALL
Java_com_amon_amonplayer_LivePush_startService(JNIEnv *env, jobject thiz) {

}